package com.koc.robot.common;

import java.lang.Math;

public class Vector
{
	// The components.
	public float x;
	public float y;
	
	/**
	 * Creates a new (1,1) Vector.
	 */
	public Vector()
	{
		x = 1;
		y = 1;
	}
	
	/**
	 * Creates a new Vector.
	 * @param x The x position / dimension
	 * @param y The y position / dimension
	 */
	public Vector(float x, float y)
	{
		this.x = x;
		this.y = y;
	}
	
	public Vector(Vector v)
	{
		this.x = v.x;
		this.y = v.y;
	}
	
	/**
	 * Gets the length of the Vector.
	 * @return The length of the Vector
	 * 
	 * This method requires sqrt() and should be used carefully.
	 */
	public float getLength()
	{
		return (float)Math.sqrt(x * x + y * y);
	}
	
	public float normalize()
	{
		float len = getLength();
		if (len > 0)
		{
			x /= len;
			y /= len;
		}
		
		return len;
	}
	
	public Vector getNormal()
	{
		return new Vector(-y, x);
	}
	
	public Vector add(Vector v)
	{
		return new Vector(x + v.x, y + v.y);
	}
	
	public Vector subtract(Vector v)
	{
		return new Vector(x - v.x, y - v.y);
	}
	
	public Vector mult(float s)
	{
		return new Vector(x * s, y * s);
	}
	
	public Vector div(float s)
	{
		return new Vector(x / s, y / s);
	}
	
	public void addLocal(Vector v)
	{
		x += v.getX();
		y += v.getY();
	}
	
	public void subtractLocal(Vector v)
	{
		x -= v.getX();
		y -= v.getY();
	}
	
	public void multLocal(float s)
	{
		x *= s;
		y *= s;
	}
	
	public void divideLocal(float s)
	{
		x /= s;
		y /= s;
	}
	
	public float dotProduct(Vector v)
	{
		return x * v.getX() + y * v.getY();
	}
	
	/**
	 * todo: find a more suitable name for this function
	 * @param v
	 * @return
	 */
	public float crossProductLen(Vector v) 
	{
		return x * v.getY() - y * v.getX();
	}
	
	public boolean equals(Vector v)
	{
		if (this == v)
		{
			return true;
		}
		
		if (Float.compare(x, v.x) != 0) return false;
		if (Float.compare(y, v.y) != 0) return false;
		return true;
	}
	
	public float getX() 
	{
		return x;
	}
	
	public float getY()
	{
		return y;
	}
	
	public void setX(float x)
	{
		this.x = x;
	}
	
	public void setY(float y)
	{
		this.y = y;
	}
}
